//-----------------------------------------------------------------------
// <copyright file="Timer.cpp" company="Microsoft">
//      Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// Module:
//      Timer.cpp
//
// Description:
//
//-----------------------------------------------------------------------

#include "Common.h"

HRESULT CTimer::Initialize()
{
    HRESULT hr = S_OK;

    if (m_hThread)
    {
        return E_FAIL;
    }

    m_hThread = CreateThread(
                    NULL,
                    0,
                    CTimer::ThreadProc,
                    (LPVOID)this,
                    0,
                    NULL);

    if (!m_hThread)
    {
        hr = HRESULT_FROM_WIN32(GetLastError());
    }

    return hr;
}

VOID CTimer::Uninitialize()
{
    if (m_hThread)
    {
        m_bStop = TRUE;
        WaitForSingleObject(m_hThread, INFINITE);
        CloseHandle(m_hThread);
        m_hThread = NULL;
    }
}

DWORD CTimer::ThreadProc(LPVOID lpParameter)
{
    CTimer *pThis = (CTimer *)lpParameter;

    while(!pThis->m_bStop)
    {
        Sleep(250);

        if (g_pDataManager)
        {
            g_pDataManager->TimerMaintenance();
        }
    }
    
    return 0;
}

